package com.woniu.user.service.impl;

import com.woniu.entity.Result;
import com.woniu.user.mapper.UserMapper;
import com.woniu.user.pojo.Fans;
import com.woniu.user.mapper.FansMapper;
import com.woniu.user.pojo.User;
import com.woniu.user.service.IFansService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 关注和粉丝表 服务实现类
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@Service
public class FansServiceImpl extends ServiceImpl<FansMapper, Fans> implements IFansService {

    @Autowired(required = false)
    private FansMapper fansMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public Result attentionByUser(String userPhone, String userToPhone) {
        User user = userMapper.selectByPhone(userPhone);
        User user1 = userMapper.selectByPhone(userToPhone);

        Fans IsFans = fansMapper.selectIsAttention(user.getId(), user1.getId());
        if (IsFans == null) {
            Fans fans = new Fans();
            fans.setSubscribe(user.getId());
            fans.setBeSubscribe(user1.getId());
            int insert = fansMapper.insert(fans);
            if (insert > 0) {
                return Result.ok(200, "关注成功");
            }
        }

        if (IsFans.getIsDeleted() == 0) {
            return Result.error(200, "你已经关注过了");
        }
        if (IsFans.getIsDeleted() == 1) {
            int update = fansMapper.updateIsDeleted(user.getId(), user1.getId());
            if (update > 0) {
                return Result.ok(200, "关注成功!");
            }
            return Result.error(500);
        }
        return Result.error(500);
    }

    @Override
    public Result rmAttentionByUser(String userPhone, String userToPhone) {
        User user = userMapper.selectByPhone(userPhone);
        User user1 = userMapper.selectByPhone(userToPhone);

        int IsFans = fansMapper.rmAttentionByUser(user.getId(), user1.getId());
        if (IsFans > 0){
            return Result.ok(200,"已取消");
        }
        return Result.error(500);
    }

}
